package wonder;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.type.TypeReference;
import org.slf4j.Logger;

import wonder.random.InitialSetup;
import wonder.settings.SettingsManager;
import wonder.tools.JsonHelper;
import wonder.tools.LoggerHelper;

/**
 * <p>
 * SWService class.
 * </p>
 * 
 * @author u211354
 * @version $Id: $
 */
public class SWService implements ISWService {
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerHelper.getLogger();
	private List<Board> maps = new ArrayList<Board>();
	private final List<BoardSet> MAP_SETS = new ArrayList<BoardSet>();

	/**
	 * <p>
	 * Constructor for SWService.
	 * </p>
	 */
	public SWService() {
		super();
		MAP_SETS.add(BoardSet.BASE);
		MAP_SETS.add(BoardSet.LEADERS);
		MAP_SETS.add(BoardSet.CITIES);
		MAP_SETS.add(BoardSet.WONDER_PACK);
		MAP_SETS.add(BoardSet.ESSEN);
		MAP_SETS.add(BoardSet.FANS);
	}

	/** {@inheritDoc} */
	@Override
	public void configure(Map<Object, Object> map) throws Exception {
		maps.clear();

		String wDesc = (String) map.get("wonders");
		if (wDesc != null && wDesc.startsWith("/")) {
			maps = JsonHelper.loadJson(
					SettingsManager.class.getResourceAsStream(wDesc),
					new TypeReference<ArrayList<Board>>() {
					});
		} else {
			maps = JsonHelper.loadJson(new FileInputStream(wDesc),
					new TypeReference<ArrayList<Board>>() {
					});
		}
	}

	/** {@inheritDoc} */
	@Override
	public void release() throws Exception {
		maps.clear();
	}

	/** {@inheritDoc} */
	@Override
	public List<Board> randomSetup(int players) throws Exception {
		List<BoardSet> sets = new ArrayList<BoardSet>();
		sets.add(BoardSet.BASE);

		return randomSetup(players, sets);
	}

	/** {@inheritDoc} */
	@Override
	public List<Board> randomSetup(int players, List<BoardSet> sets)
			throws Exception {
		List<Board> rc = InitialSetup.randomize(players, sets);
		return rc;
	}

	/** {@inheritDoc} */
	@Override
	public List<BoardSet> getSetList() {
		return MAP_SETS;
	}

	@Override
	public List<Board> getBoards() {
		return maps;
	}

	@Override
	public void setAvailability(BoardSet set, boolean available) {
		for (Board m : maps) {
			if (m.getSet().equals(set)) {
				m.setAvailable(available);
			}
		}
	}

	@Override
	public void setAvailability(String map, boolean available) {
		for (Board m : maps) {
			if (m.getName().equals(map)) {
				m.setAvailable(available);
			}
		}
	}

	@Override
	public void setAvailability(Board wm, boolean available) {
		String map = wm.getName();
		BoardSide side = wm.getSide();

		for (Board m : maps) {
			if (m.getName().equals(map) && m.getSide().equals(side)) {
				m.setAvailable(available);
			}
		}
	}

	@Override
	public int countAvailable() {
		int rc = 0;
		for (Board m : maps) {
			if (m.isAvailable()) {
				rc++;
			}
		}

		return rc;
	}

	@Override
	public Board getBoard(String map, BoardSide side) {
		for (Board m : maps) {
			if (m.getName().equals(map) && m.getSide().equals(side)) {
				return m;
			}
		}
		return null;
	}
}
